package com.cgt.xiaotao.user.controller;

import com.cgt.common.domain.dto.LoginFormDTO;
import com.cgt.common.domain.dto.LoginUserDTO;
import com.cgt.common.utils.R;
import com.cgt.xiaotao.user.domain.dto.UserAddressDTO;
import com.cgt.xiaotao.user.domain.dto.UserDTO;
import com.cgt.xiaotao.user.domain.query.UserQuery;
import com.cgt.xiaotao.user.domain.vo.UserVO;
import com.cgt.xiaotao.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Api(tags = "用户接口")
@Slf4j
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {
    private final IUserService userService;

    @ApiOperation("测试")
    @GetMapping("test")
    public String test() {
        return userService.test();
    }

    @ApiOperation("查询用户详情")
    @GetMapping("/info")
    public R<UserVO> queryUser() {
        try {
            UserVO vo = userService.queryUser();
            return R.ok(vo);
        } catch (Exception e) {
            log.error(e.toString());
            return R.fail(e.toString());
        }
    }

    /**
     * 根据手机号查询用户id
     *
     * @param phone 手机号
     * @return 用户id
     */
    @ApiOperation("根据手机号查询用户id")
    @GetMapping("/ids")
    Long exchangeUserIdWithPhone(@RequestParam("phone") String phone) {
        return userService.exchangeUserIdWithPhone(phone);
    }

    /**
     * 登录接口
     *
     * @param loginDTO 登录信息
     * @param isStaff  是否是员工
     * @return 用户详情
     */
    @PostMapping("/detail/{isStaff}")
    LoginUserDTO queryUserDetail(@RequestBody LoginFormDTO loginDTO, @PathVariable("isStaff") boolean isStaff) {
        return userService.queryUserDetail(loginDTO, isStaff);
    }

    @ApiOperation("更新用户头像")
    @PostMapping("/updateAvatar")
    public R<String> updateAvatar(@ApiParam(name = "file", value = "文件", required = true)
                                  @RequestParam("file") MultipartFile file) {
        try {
            userService.updateAvatar(file);
            return R.ok("更新成功");
        } catch (Exception e) {
            return R.fail(e.toString());
        }
    }

    @ApiOperation("更新用户信息")
    @PutMapping("/update")
    public R<String> updateUser(@RequestBody UserQuery query) {
        try {
            userService.updateUser(query);
            return R.ok("更新成功");
        } catch (Exception e) {
            return R.fail(e.toString());
        }
    }
}
